/*  -------------------------- (C) COPYRIGHT 2022 Fortiortech ShenZhen ---------------------------*/
/**
 * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
 * @file      xxx.c
 * @author    Fortiortech  Appliction Team
 * @since     Create:2022-07-14
 * @date      Last modify:2022-07-14
 * @note      Last modify author is Leo.li
 * @brief
 */

/* Includes -------------------------------------------------------------------------------------*/
#include <FU68xx_2.h>
#include <Myproject.h>

/* Private variables ----------------------------------------------------------------------------*/

/**
 * @brief       TIM0初始化配置
 * @data        2022/08/12
 */
void TIM0_Init(void)
{
}

/**
 * @brief       TIM1初始化配置
 * @data        2022/08/12
 */
void TIM1_Init(void)
{
}

/**
 * @brief       TIM2初始化配置
 * @data        2022/08/12
 */
void TIM2_Init(void)
{
  /*  -------------------------------------------------------------------------------------------------
      先停止计数，配置完寄存器后，最后启动计数
      -------------------------------------------------------------------------------------------------*/
  ClrBit(TIM2_CR1, T2EN); // 0，停止计数；1,使能计数
  ClrBit(PH_SEL, T2SEL);  // 0-不连接到端口GP11；1-连接到端口GP11
  /*  -------------------------------------------------------------------------------------------------
      时钟分频设置(T2PSC)
      000:cpuclk(24MHz)           001:cpuclk/2^1(12MHz)   010:cpuclk/2^2(6MHz)    011:cpuclk/2^3(3MHz)
      100:cpuclk/2^4(1.5MHz)  101:cpuclk/2^5(750KHz)  110:cpuclk/2^6(375KHz)  111:cpuclk/2^7(187.5KHz)
      -------------------------------------------------------------------------------------------------*/
  SetReg(TIM2_CR0, T2PSC0 | T2PSC1 | T2PSC2, T2PSC0 | T2PSC1);
  /*  -------------------------------------------------------------------------------------------------
      /模式选择
      T2MODE1，T2MODE0
      00--输入Timer模式；01--输出模式
      10--输入Count模式；11--QEP或者RSD模式
      -------------------------------------------------------------------------------------------------*/
  SetReg(TIM2_CR0, T2MOD0 | T2MOD1, T2MOD0);

  /*  -------------------------------------------------------------------------------------------------
      清除中断标志位
      禁止PWM周期检测中断使能
      使能计数器上溢中断使能
      -------------------------------------------------------------------------------------------------*/
  ClrBit(TIM2_CR0, T2CES);              // 清零脉冲计数器不使能
  ClrBit(TIM2_CR1, T2IR | T2IF | T2IP); // 清零中断标志位
  SetBit(TIM2_CR1, T2IFE | T2IRE);      // 上溢中断使能
  /*  -------------------------------------------------------------------------------------------------
      配置周期值、比较值、计数值
      禁止PWM周期检测中断使能
      使能计数器上溢中断使能
      -------------------------------------------------------------------------------------------------*/
  //TIM2__ARR = 18000; // //3M分频，1/3us,24000对应延迟6ms
	TIM2__ARR = 18000;  //6ms 
  TIM2__DR = TIM2__ARR;
  TIM2__CNTR = 0;
  /*-----------启动计数------------------------------------------------*/
  ClrBit(TIM2_CR1, T2EN); // 启动计数
}

/**
 * @brief       TIM3初始化配置
 * @data        2022/08/12
 */
void TIM3_Init(void)
{
  /*-------------------------------------------------------------------------------------------------
    先停止计数，配置完寄存器后，最后启动计数
  -------------------------------------------------------------------------------------------------*/
  ClrBit(TIM3_CR1, T3EN); // 0-停止计数；1-使能计数
                          //	SetBit(PH_SEL, T3SEL);				//0-不连接到端口GP11；1-连接到端口GP11
                          /*-------------------------------------------------------------------------------------------------
                            时钟分频设置(T2PSC)
                            000:cpuclk(24MHz)			001:cpuclk/2^1(12MHz)	010:cpuclk/2^2(6MHz)	011:cpuclk/2^3(3MHz)
                            100:cpuclk/2^4(1.5MHz)	101:cpuclk/2^5(750KHz)	110:cpuclk/2^6(375KHz)	111:cpuclk/2^7(187.5KHz)
                          -------------------------------------------------------------------------------------------------*/
  SetReg(TIM3_CR0, T3PSC1 | T3PSC0, T3PSC1 | T3PSC0);

  /*-------------------------------------------------------------------------------------------------
    /模式选择
    0-输入模式
    1-输出模式
  -------------------------------------------------------------------------------------------------*/
  SetBit(TIM3_CR0, T3MOD);

  ClrBit(TIM3_CR0, T3OCM);

  /*-------------------------------------------------------------------------------------------------
    /滤波时间
    00-不滤波；01-8个时钟周期
    10-16个时钟周期；11-32个时钟周期
  -------------------------------------------------------------------------------------------------*/
  SetReg(TIM3_CR1, T3FE1 | T3FE0, T3FE0);
  /*-------------------------------------------------------------------------------------------------
    清除中断标志位
    禁止PWM周期检测中断使能
    使能计数器上溢中断使能
  -------------------------------------------------------------------------------------------------*/
  ClrBit(TIM3_CR1, T3IR | T3IF | T3IP); // 清除中断标志位
  SetBit(TIM3_CR1, T3IFE);              // 输入有效边沿变化中断使能和基本计数器上溢使能
  SetBit(TIM3_CR0, T3IRE);              // 输入有效边沿变化中断使能和基本计数器上溢使能
                                        /*-------------------------------------------------------------------------------------------------
                                          定时器2中断优先级配置及芯片中断总使能
                                          PTIM31-PTIM30，中断优先级控制值从0-3依次表示优先级从最低到最高，共4级优化级控制
                                          EA,芯片中断总使能
                                        -------------------------------------------------------------------------------------------------*/
  PTIM31 = 0;
  PTIM30 = 0; // TIM2/3中断优先级别为2
  EA = 1;

  /*-------------------------------------------------------------------------------------------------
    配置周期值、比较值、计数值
  -------------------------------------------------------------------------------------------------*/
  TIM3__CNTR = 0;
  TIM3__ARR = ledCt1.ledCycleFactor; // 周期调节闪烁
  TIM3__DR = 0;                      // 占空比
  /*-----------关闭计数------------------------------------------------*/
  SetBit(TIM3_CR1, T3EN); // 打开计数器
}

/**
 * @brief       TIM4初始化配置
 * @data        2022/08/12
 */
void TIM4_Init(void)
{
  /*  -------------------------------------------------------------------------------------------------
      先停止计数，配置完寄存器后，最后启动计数
      -------------------------------------------------------------------------------------------------*/
  ClrBit(TIM4_CR1, T4EN); // 0-停止计数；1-使能计数
  SetBit(PH_SEL, T4SEL);  // 0-不连接到端口GP01；1-连接到端口GP01
  /*  -------------------------------------------------------------------------------------------------
      时钟分频设置(T2PSC)
      000:cpuclk(24MHz)         001:cpuclk/2^1(12MHz)   010:cpuclk/2^2(6MHz)    011:cpuclk/2^3(3MHz)
      100:cpuclk/2^4(1.5MHz)    101:cpuclk/2^5(750KHz)  110:cpuclk/2^6(375KHz)  111:cpuclk/2^7(187.5KHz)
      -------------------------------------------------------------------------------------------------*/
  SetReg(TIM4_CR0, T4PSC0 | T4PSC1 | T4PSC2, T4PSC0 | T4PSC1 | T4PSC2);
  /*  -------------------------------------------------------------------------------------------------
      /模式选择
      0-输入模式
      1-输出模式
      -------------------------------------------------------------------------------------------------*/
  ClrBit(TIM4_CR0, T4MOD);
  ClrBit(TIM4_CR0, T4OCM); //     定时器比较匹配/捕获脉宽模式配置
  /*  -------------------------------------------------------------------------------------------------
      /滤波时间
      00-不滤波；01-8个时钟周期
      10-16个时钟周期；11-32个时钟周期
      -------------------------------------------------------------------------------------------------*/
  SetReg(TIM4_CR1, T4FE1 | T4FE0, T4FE0);
  /*  -------------------------------------------------------------------------------------------------
      清除中断标志位
      禁止PWM周期检测中断使能
      使能计数器上溢中断使能
      -------------------------------------------------------------------------------------------------*/
  ClrBit(TIM4_CR1, T4IR | T4IF | T4IP); // 清除中断标志位
  SetBit(TIM4_CR1, T4IPE | T4IFE);      // 输入有效边沿变化中断使能和基本计数器上溢使能
  /*  -------------------------------------------------------------------------------------------------


      与TIM1MS中断一起配置优先级，修改优先级时应注意
      -------------------------------------------------------------------------------------------------*/
  //  PTIM4S1 = 0;
  //  PTIM4S0 = 1;                                                           // TIM4/5中断优先级别为1
  //  EA = 1;
  /*  -------------------------------------------------------------------------------------------------
      配置周期值、比较值、计数值
      -------------------------------------------------------------------------------------------------*/
  TIM4__CNTR = 0;
  TIM4__DR = 0;
  TIM4__ARR = 0;
  /*-----------启动计数------------------------------------------------*/
  SetBit(TIM4_CR1, T4EN); // 使能计数器，启动计数
}

/**
 * @brief       TIM1MS初始化配置
 * @data        2022/08/12
 */
void TIM1ms_Init(void)
{
  // SYST_ARR = 24000;     //计算公式：24M/SYST_ARR；例 24M/24000=1000Hz    SYST_ARR为SYS_TICK的重载值
  PTIM4S1 = 0;
  PTIM4S0 = 1; // TIM4/5中断优先级别为1
  EA = 1;
  SetBit(DRV_SR, SYSTIE); // 使能1ms定时中断 ;
}
